
import {PayloadAction, createSlice} from "@reduxjs/toolkit";
import { RootState } from "../../app/types";

// 为 slice 创建一个类型
interface CounterState {
    value: number;
}

// 使用该类型初始化 state
const initialState: CounterState = {
    value: 0,
}


export const counterSlice = createSlice({
    name: "counter",
    initialState,
    reducers: {
        increment: state => {
            state.value += 1;
        },
        decrement: state => {
            state.value -= 1;
        },
        incrementByAmount: (state, action: PayloadAction<number>) => {
            state.value += action.payload;
        }
    }
})

export const {increment, decrement, incrementByAmount} = counterSlice.actions;

export const selectCount = (state: RootState) => {
    return state.counter.value;
}

export default counterSlice.reducer;